package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    @Override
    public BedVo readBed(Long id) {
        //根据id查询对象
        BedVo bedVo = BeanUtil.toBean(bedMapper.readBed(id), BedVo.class);
        return bedVo;
    }

    @Override
    public void addBed(BedDto bedDto) {
        //因为新增的是床位信息，所有要转为bed来进行新增
        Bed bed = BeanUtil.toBean(bedDto, Bed.class);
        //写死状态
        bed.setBedStatus(0);
        //新加创建时间
        bed.setCreateTime(LocalDateTime.now());
        //新加更新时间
        bed.setUpdateTime(LocalDateTime.now());
        bed.setCreateBy(1L);
        bed.setUpdateBy(1L);
        log.info("新增床位：{}",bed);

        try {
            bedMapper.addBed(bed);
        } catch (Exception e) {//自定义好了异常，可以直接来拿用
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }
    }

    @Override
    public void delBad(Long id) {
        //根据传进来的id来删除床位
        bedMapper.del(id);
    }

    @Override
    public void putBed(BedDto bedDto) {
        //需要根据bed里面的参数修改数据，所有需要强转
        Bed bed = BeanUtil.toBean(bedDto, Bed.class);
        bed.setCreateTime(LocalDateTime.now());
        bed.setCreateBy(1L);
        log.info("修改数据：{}",bed);
        bedMapper.upBed(bed);
    }

}

